package com.demo.supplier.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.supplier.entity.FileInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 文件信息Mapper接口
 *
 * @author demo
 * @since 2024-12-19
 */
@Mapper
public interface FileInfoMapper extends BaseMapper<FileInfo> {

    /**
     * 根据文件ID列表批量查询文件信息
     *
     * @param fileIds 文件ID列表
     * @return 文件信息列表
     */
    @Select("<script>" +
            "SELECT * FROM T_FILE_INFO WHERE ID IN " +
            "<foreach item='item' index='index' collection='fileIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            " AND DELETED = 0 AND STATUS = 1" +
            "</script>")
    List<FileInfo> selectByIds(@Param("fileIds") List<Long> fileIds);

    /**
     * 检查文件是否被业务表使用
     *
     * @param fileId 文件ID
     * @return 使用次数
     */
    @Select("SELECT (" +
            "SELECT COUNT(*) FROM T_SUPPLIER_INFO WHERE (LOGO_FILE_ID = #{fileId} OR LICENSE_FILE_ID = #{fileId} OR CERTIFICATE_FILE_ID = #{fileId}) AND DELETED = 0" +
            ") + (" +
            "SELECT COUNT(*) FROM T_SYS_USER WHERE AVATAR_FILE_ID = #{fileId} AND DELETED = 0" +
            ") AS usage_count FROM DUAL")
    int checkFileUsage(@Param("fileId") Long fileId);
}